package com.example.service;

import com.example.event.DAO.EventDAO;
import com.example.history.DAO.HistoryDAO;
import com.example.historyHour.DAO.HistoryHourDAO;
import com.example.mytest.DAO.MessageDAO;
import com.example.mytest.Entity.MessageEntity;
import com.example.mytest.DAO.DeviceDAO;
import com.example.mytest.DAO.DeviceOnlineRecordDAO;
import com.example.tools.HttpClient;
import com.example.tools.Uniqid;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Studio on 2017/5/19.
 */
public class HandleOnline {

    private static SimpleDateFormat date_format1 = new SimpleDateFormat("MM");
    private static SimpleDateFormat date_format2 = new SimpleDateFormat("dd");
    private static SimpleDateFormat date_format3 = new SimpleDateFormat("HH");

    private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private String url = "http://192.168.25.105/openapi/public/index.php/msg/send";
    private String params = "type=1&did=";

    private DeviceDAO deviceDAO;
    private HistoryDAO historyDAO;
    private DeviceOnlineRecordDAO deviceOnlineRecordDAO;
    private EventDAO eventDAO;
    private HistoryHourDAO historyHourDAO;
    private MessageDAO messageDAO;

    private int m;
    private int d;
    private int h;
    private int result;

    public HandleOnline(DeviceDAO deviceDAO, HistoryDAO historyDAO, DeviceOnlineRecordDAO deviceOnlineRecordDAO, EventDAO eventDAO, HistoryHourDAO historyHourDAO, MessageDAO messageDAO){
        this.deviceDAO = deviceDAO;
        this.historyDAO = historyDAO;
        this.deviceOnlineRecordDAO = deviceOnlineRecordDAO;
        this.eventDAO = eventDAO;
        this.historyHourDAO = historyHourDAO;
        this.messageDAO = messageDAO;
    }



    public void handle_online(String device_id,long created , long created_hour) throws Exception{
         result = deviceDAO.updateOnline(device_id);
        if(result == 1) {
             m = Integer.parseInt(date_format1.format(System.currentTimeMillis()));
             d = Integer.parseInt(date_format2.format(System.currentTimeMillis()));
             h = Integer.parseInt(date_format3.format(System.currentTimeMillis()));

            deviceOnlineRecordDAO.insertOnline(Uniqid.getId(),device_id,df.format(new Date()),0);


            if(m<10){
                eventDAO.updateOnline0(m,device_id,created);
                historyHourDAO.insterHistory0(m, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id);

            }else {
                eventDAO.updateOnline1(m,device_id,created);
                historyHourDAO.insterHistory1(m, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id, created_hour, device_id);

            }

            if (m < 10 && d < 10 && h < 10) {
                historyDAO.insterHistory0(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);

            } else if (m < 10 && d < 10 && h >= 10) {
                historyDAO.insterHistory1(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            } else if (m < 10 && d >= 10 && h < 10) {
                historyDAO.insterHistory2(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            } else if (m < 10 && d >= 10 && h >= 10) {
                historyDAO.insterHistory3(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            } else if (m >= 10 && d < 10 && h < 10) {
                historyDAO.insterHistory4(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            } else if (m >= 10 && d < 10 && h >= 10) {
                historyDAO.insterHistory5(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            } else if (m >= 10 && d >= 10 && h < 10) {
                historyDAO.insterHistory6(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            } else if (m >= 10 && d >= 10 && h >= 10) {
                historyDAO.insterHistory7(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            }

        }
    }


    public void handle_online(String device_id,long created) throws Exception{
        result = deviceDAO.updateOnline(device_id);
        if(result == 1) {
            m = Integer.parseInt(date_format1.format(System.currentTimeMillis()));
            d = Integer.parseInt(date_format2.format(System.currentTimeMillis()));
            h = Integer.parseInt(date_format3.format(System.currentTimeMillis()));

            deviceOnlineRecordDAO.insertOnline(Uniqid.getId(),device_id,df.format(new Date()),0);


            if(m<10){
                eventDAO.updateOnline0(m,device_id,created);
            }else {
                eventDAO.updateOnline1(m,device_id,created);
            }

            if (m < 10 && d < 10 && h < 10) {
                historyDAO.insterHistory0(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);

            } else if (m < 10 && d < 10 && h >= 10) {
                historyDAO.insterHistory1(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            } else if (m < 10 && d >= 10 && h < 10) {
                historyDAO.insterHistory2(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            } else if (m < 10 && d >= 10 && h >= 10) {
                historyDAO.insterHistory3(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            } else if (m >= 10 && d < 10 && h < 10) {
                historyDAO.insterHistory4(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            } else if (m >= 10 && d < 10 && h >= 10) {
                historyDAO.insterHistory5(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            } else if (m >= 10 && d >= 10 && h < 10) {
                historyDAO.insterHistory6(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            } else if (m >= 10 && d >= 10 && h >= 10) {
                historyDAO.insterHistory7(m, d, h, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id, created, device_id);
            }

            //发短信通知,请求PHP端

            MessageEntity messageEntity = messageDAO.getOne(device_id);

            if(messageEntity != null){
                if(messageEntity.timeOut()){
                    HttpClient.sendPost(url,params+device_id);
                }
            }else{
                HttpClient.sendPost(url,params+device_id);
            }

        }
    }





}
